package com.ksp.wke.kspa01;

import static com.ksp.com.KSPCOM_BasConst.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ksp.api.SpringUtil;

/**
 * 
 * @author anye
 *
 */
public class KSPA01InsThd implements Runnable{
	
	private Logger log = LoggerFactory.getLogger(KSPA01InsThd.class);

	ConcurrentLinkedQueue<Map<String, Object>> artScaQue;
	
	private static AtomicInteger totalSca = new AtomicInteger(0);
	
	public static ReentrantLock dealLock = new ReentrantLock(true);
	
	public KSPA01InsThd(){
	}
	
	public KSPA01InsThd(ConcurrentLinkedQueue<Map<String, Object>> scaque) {
		artScaQue = scaque;
	}
	
	@Override
	public void run() {
		while(!artScaQue.isEmpty()) {
			try {
				dealLock.lock();
				Iterator<Map<String, Object>> iterator = artScaQue.iterator();
				while (iterator.hasNext()) {
					dealArtSca(iterator.next());
					artScaQue.poll();
				} 
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				dealLock.unlock();
			}
		}
		
	}
	
	void dealArtSca(Map<String, Object> wPamMap) {
		
		//System.out.println("队列线程处理开始：");
		KSPA01Mapper kspa01Mapper = SpringUtil.getBean(KSPA01Mapper.class);
		int wCreTtlTch = ORG_TCH;
		List<Map<String, Object>> wMaxTtlTchLst = kspa01Mapper.qryMaxTtlTch(wPamMap);
		if (wMaxTtlTchLst != null && wMaxTtlTchLst.size() > 0) {
			Map<String, Object> wMaxTtlTchMap = wMaxTtlTchLst.get(0);
			if (wMaxTtlTchMap != null) {
				wCreTtlTch = Integer.parseInt(wMaxTtlTchMap.get("TTL_TCH").toString()) + 1;
			}
		}
		
		wPamMap.put("TTL_TCH", wCreTtlTch);
		int insArtTtl = kspa01Mapper.insArtTtl(wPamMap);
		int insArtCon = kspa01Mapper.insArtCon(wPamMap);
		if (insArtTtl > 0 && insArtCon > 0) {
			log.info("新增文章成功，id" + wCreTtlTch + "线程：" + Thread.currentThread().getId());
		}
	}
}
